/**
 * made in china by xufeng
 * 2007-6-12 下午05:00:13
 * 
 */
package net.litxuf.Util;

import org.apache.log4j.Logger;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;

/**
 * @author XuFeng
 *
 */
public class TimeUtil {
 private static final Logger logger= Logger.getLogger(TimeUtil.class.getName());

	public static Timestamp transAllStingToDate(String dateStr){
		String reg="[^\\d]+";
		Timestamp timestamp=null;
		if(dateStr!=null&&dateStr.length()>7){
			String[] dates=dateStr.split(reg);
			if(dates!=null&&dates.length==3){
				
				Calendar ca = Calendar.getInstance();
				if(dates[0].length()==0||dates[1].length()==0||dates[2].length()==0) return null;
				 
				 int year=Integer.parseInt(dates[0]);
				 int month=Integer.parseInt(dates[1]);
				 int day=Integer.parseInt(dates[2]);
				    if(year<9999&&year>1753){
					ca.set(Calendar.YEAR, Integer.parseInt(dates[0]));
					ca.set(Calendar.MONTH, Integer.parseInt(dates[1])-1);
					ca.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[2]));
				}
				
			 timestamp=new Timestamp(ca.getTimeInMillis());
//				
			}
		}
		
		
		return timestamp;
		
		
	}
/**
 * 格式化数据库中 格式为"yyyy年MM月dd日" 转化为 Timestamp 
 * @param dateStr
 * @return
 */
    public static Timestamp transStingToDate(String dateStr){

    String reg="[\\d]{4}年[\\d]{1,2}月[\\d]{1,2}日";
	Timestamp timestamp=null;
	if(dateStr!=null&&dateStr.matches(reg))
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
		try {
			Date date=sdf.parse(dateStr);
			timestamp=new Timestamp(date.getTime());

		} catch (ParseException e) {
			logger.debug(e.toString());
			e.printStackTrace();
		}

	}

	return timestamp;

	}
    /***
     * 文本的格式为"yyyy-MM-dd"
     * @param dateStr
     * @return
     */
public static Timestamp StingToDate(String dateStr){
	String reg="[\\d]{4}-[\\d]{1,2}-[\\d]{1,2}";
	Timestamp timestamp=null;
	if(dateStr!=null&&dateStr.matches(reg))
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date=sdf.parse(dateStr);
			timestamp=new Timestamp(date.getTime());

		} catch (ParseException e) {
            logger.debug(e.toString());
			e.printStackTrace();
		}

	}else{
		timestamp=new Timestamp(System.currentTimeMillis());
	}
	
	return timestamp;
}

    public static String TimestampToString(Timestamp timestamp,String formatStr){
    		String dateStr="";
    		if(timestamp==null)return "";
    	try {
			SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
			dateStr=sdf.format(timestamp);
		} catch (Exception e) {
			 logger.debug(e.toString());
			e.printStackTrace();
		}
		return dateStr;
    }
    
    
public static void main(String[] args) {
//	transStingToDate("sdf 9 sd1f");
	String s[]="January 01, 1992Q".split("[^\\d]+");
	System.out.println(s.length);
	System.out.println(s[0]);
	System.out.println(s[1]);
	System.out.println(s[2]);
    Timestamp now=new Timestamp(System.currentTimeMillis());
    System.out.println(TimestampToString(now,"yyyy--MM---dd"));
//	String s="2007-5-9";
//	String sw="2007年1月9日";
//	System.out.println(" sw"+ sw.trim().length());
//	System.out.print(s.trim().length());
}

}
